using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using StupidSpaceThing;

namespace RacerGame
{
    class Background
    {
        private static GraphicsDevice device;
        private static SpriteBatch spriteBatch;
        private static Texture2D background, foreground;
        private static float bgSpeed = 0.5f, fgSpeed = 0.2f;
        private static int bgPos = 0, fgPos = 0;
        
        
        public static void Init(GraphicsDevice device)
        {
            Background.device = device;
            spriteBatch = new SpriteBatch(device);
        }

        public static void LoadTextures(Texture2D background, Texture2D foreground)
        {
            Background.background = background;
            Background.foreground = foreground;
            
        }

        public static void Draw(RealGameTime gameTime)
        {
            
            int width = device.DisplayMode.Width;
            int height = device.DisplayMode.Height;
            float time = (float)gameTime.ElapsedRealTime.TotalMilliseconds;
            bgPos += (int) (time * bgSpeed);
            fgPos += (int) (time * fgSpeed);
            if (bgPos >= background.Width)
                bgPos = bgPos - background.Width;
            if (fgPos >= foreground.Width)
                fgPos = fgPos - foreground.Width;
            //Rectangle screenRect = new Rectangle(0,0,width,height);
            device.RenderState.DepthBufferEnable = false;
            
            spriteBatch.Begin();
            spriteBatch.Draw(background, new Vector2(bgPos - background.Width ,0),  Color.White);
            spriteBatch.Draw(background, new Vector2(bgPos, 0), Color.White);
            spriteBatch.End();
            device.RenderState.BlendFunction = BlendFunction.Min;
            spriteBatch.Begin();
            spriteBatch.Draw(foreground, new Vector2(fgPos - foreground.Width, 0), Color.White);
            spriteBatch.Draw(foreground, new Vector2(fgPos, 0), Color.White);            
            spriteBatch.End();

            
            device.RenderState.BlendFunction = BlendFunction.Add;
            device.RenderState.DepthBufferEnable = true;

        }
    }
}
